package com.jsls.netspri.controller.user;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.jsls.netspri.dao.UserDao;
import com.jsls.netspri.entity.User;

@Controller
@Transactional
@RequestMapping("/user")
@SessionAttributes("User")
public class UserController {

	@Resource
	private UserDao userDao;

	//首页
	@RequestMapping(value="/home",method=RequestMethod.GET)
	public String toMain(){
		return "/user/home";
	}
	
	//显示个人信息
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public String toUpdate(@PathVariable(value="id")Integer id,Model model){
		
		User user = userDao.findById(id);
		model.addAttribute("updateUser", user);
		
		return "/user/updateuser";
	}
	
	//修改个人信息
	@RequestMapping(value="/{id}",method=RequestMethod.PUT)
	public String updateUser(@PathVariable(value="id")Integer id, User updateUser,Model model){
		userDao.updateUser(updateUser);
		User user = userDao.findById(id);
		model.addAttribute("User", user);
		return "redirect:/user/home";
	}
	
	//登出
	@RequestMapping(value="/logout",method=RequestMethod.GET )
	private String logout(HttpSession session){
		session.setMaxInactiveInterval(0);
		session.invalidate();
		
		return "redirect:/login/tologin";
	}
	
}
